package warmachinesystem.presentation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import warmachinesystem.provider.WarmachineSystemItemProviderAdapterFactory;

public class GameSystemView extends ViewPart {

	public static final String ID =
	      "warmachinesystem.presentation.GameSystemViewID";
	private TreeViewer viewer;
	
	public GameSystemView() {
		 super();
	}

	// the listener we register with the selection service 
	private ISelectionListener listener = new ISelectionListener() {
		public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
			//TODO: in questo punto deve essere aggiutno il codice per gestire il refresh da un progetto all'altro
        	//per aggiornare il contenuto della view
        	
        	if (selection instanceof IStructuredSelection){
        		IStructuredSelection ssel = (IStructuredSelection)selection;
        		if (ssel.getFirstElement() instanceof IFile && ssel.size() == 1){
        			IFile file = (IFile) ssel.getFirstElement();
        			ResourceSet rs = new ResourceSetImpl();
        			URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), false);

        			Resource res = rs.getResource(uri, true);
        			viewer.setInput(res.getContents().get(0));
        		}
        			
        	}
        	
        	return;
			
		}
	};
	
	@Override
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, 0);

		AdapterFactory gameSystemAdapterFactory = new WarmachineSystemItemProviderAdapterFactory();
		
		List <AdapterFactory> fList = new ArrayList<AdapterFactory>();
		fList.add(gameSystemAdapterFactory);
		ComposedAdapterFactory composedFactory = new ComposedAdapterFactory(fList);
		viewer.setContentProvider(new AdapterFactoryContentProvider(composedFactory));
		viewer.setLabelProvider(new AdapterFactoryLabelProvider(composedFactory));

		getSite().getPage().addSelectionListener(listener);
		
        viewer.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
            	System.out.println(event);
            }
        });
        

	}

	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

}
